package com.romotc.qqrobot.webqq.qqInterfaceCall;

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.SysConstant;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.Ptlogin2_Check_Processor;
import com.romotc.qqrobot.webqq.util.QQRandomUtil;
import com.romotc.scache.httpreq;
/**
 * 调用QQ验证码接口
 * @author romotc
 *
 */
public class Ptlogin2_Check_InterCall extends QQInterfaceCall {

	private static Ptlogin2_Check_InterCall instance;
	public static Ptlogin2_Check_InterCall getInstance() {
		if(instance == null)
			instance = new Ptlogin2_Check_InterCall();
		return instance;
	}
	
	@Override
	public JSONObject preCall(QQLogedin loginQQ, JSONObject reqDataObj)
			throws Exception {
		String cookie = loginQQ.getCookieString();
		if(StringUtils.isBlank(cookie) || cookie.indexOf("pgv_pvid") == -1) {
			String pgv_pvid = QQRandomUtil.getRandomPvid();
			String pgv_info = "ssid=" + QQRandomUtil.getRandomSSid();
			cookie = "pgv_pvid=" + pgv_pvid + "; pgv_info=pgvReferrer=&" + pgv_info;
			loginQQ.setCookieString(cookie);
		}
		return reqDataObj;
	}

	@Override
	public boolean hasPreCall(QQLogedin loginQQ, JSONObject reqDataObj) {

		return true;
	}

	@Override
	public ArrayList<String> myReqHeaders(int reqBodyLen, QQLogedin loginQQ,JSONObject reqDataObj) {
//		Accept:*/*
//		Accept-Charset:UTF-8,*;q=0.5
//		Accept-Encoding:gzip,deflate,sdch
//		Accept-Language:zh-CN,zh;q=0.8
//		Connection:keep-alive
//		Cookie:ptcz=0cdccd26c3fff8dc8a44a8519a020e5387a00d5600f3e14ebf74a8ef12804836; ptui_loginuin=97190865; pt2gguin=o2506733417; o_cookie=2506733417; pgv_pvid=6635240140; pgv_info=pgvReferrer=&ssid=s5396571412; uikey=60e84cc7573cd89f46e334224908bf003d8ba231262aa958e6de1e4332703183; chkuin=97190865
//		Host:check.ptlogin2.qq.com
//		Referer:http://ui.ptlogin2.qq.com/cgi-bin/login?target=self&style=5&mibao_css=m_webqq&appid=1003903&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fweb.qq.com%2Floginproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=20120320001
//		User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.83 Safari/535.11
		
		ArrayList<String> s = new ArrayList<String>();

		s.add("Accept:*/*");
		s.add("Accept-Charset:UTF-8,*;q=0.5");
		s.add("Accept-Encoding:gzip,deflate,sdch");
		s.add("Accept-Language:zh-CN,zh;q=0.8");
		s.add("Connection:keep-alive");
		s.add("Cookie:" + loginQQ.getCookieString());
		s.add("Host:check.ptlogin2.qq.com");
		s.add("Referer:http://ui.ptlogin2.qq.com/cgi-bin/login?target=self&style=5&mibao_css=m_webqq" +
				"&appid=" + WebQQConstant.APPID +
				"&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fweb.qq.com%2Floginproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=" + WebQQConstant.T);
		s.add("User-Agent:" + SysConstant.User_Agent);
		return s;		
	}

	@Override
	public String buildReqBody(JSONObject reqDataObj, QQLogedin loginQQ)
			throws Exception {
		return null;
	}

	@Override
	public int getLogLevel() {
		return 1001;
	}

	@Override
	public int getReq_method() {
		return httpreq.REQUEST_GET;
	}

	@Override
	public String getReqUrl(JSONObject reqDataObj, QQLogedin loginQQ) throws Exception {
		return WebQQConstant.URL_PTLOGIN2_CHECK 
				+ "?uin=" + reqDataObj.getString(Ptlogin2_Login_InterCall.Key_Login_Param_QQ) + "&appid=" + WebQQConstant.APPID 
				+ "&r=" + QQRandomUtil.webQQRandom();
	}
	
	
	@Override
	public Class getResClassType() {
		return null;
	}
	
	
	
	public static void main(String[] args) throws Exception{
		Ptlogin2_Check_InterCall ptcheck = new Ptlogin2_Check_InterCall();
		QQLogedin curQqLogedIn = new QQLogedin();
		JSONObject reqDataObj = new JSONObject();
		reqDataObj.put(WebQQConstant.REQUEST_PARAMS_tuin, "97190867");
		ptcheck.call(curQqLogedIn, reqDataObj, Ptlogin2_Check_Processor.getInstance());
	}

}
